package com.jyhs.maven.dao.impl;


import com.jyhs.maven.dao.BaseDao;
import com.jyhs.maven.dao.UserDao;
import com.jyhs.maven.util.ReflectUtil;
import com.jyhs.maven.entity.User;
import com.jyhs.maven.entity.dto.UserDto;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public List<User> getUserList() throws SQLException, ClassNotFoundException, ParseException, IllegalAccessException {


        getConnection();
        String sql = "SELECT * FROM user";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        List<User> users = new ArrayList<>();
        while (rs.next()) {
            User user = new User();
//            UserDto userDto = new UserDto();
            ReflectUtil.resultSetToObject(rs,user);

//            user.setId(rs.getLong("id"));
//            user.setName(rs.getString("name"));
//            user.setCardNumber(rs.getString("cardNumber"));
//            user.setPhone(rs.getString("phone"));
//            user.setBalance(rs.getBigDecimal("balance"));

            users.add(user);
        }

        return users;

    }

    @Override
    public User getUser(Long id) throws SQLException, ClassNotFoundException {
        String sql = "SELECT * FROM user where id = ?";
        ResultSet rs = getSelect(sql, id);
        User user= new User();;
        while (rs.next()) {

            user.setId(rs.getLong("id"));
            user.setName(rs.getString("name"));
            user.setCardNumber(rs.getString("cardNumber"));
            user.setPhone(rs.getString("phone"));
            user.setBalance(rs.getBigDecimal("balance"));

        }
        return user;
    }

    @Override
    public int deleteUser(List<Long> id)   {
        return 0;
    }
}
